<?php
/*
Plugin Name: Quiz Animation
Plugin URI: https://www.google.it/search?q=aspinall%20vincenzo%20giacchina&rct=j
Description: Test Quiz 
Author: Vincenzo Giacchina
Version: 1.0
Author URI: https://www.google.it/search?q=aspinall%20vincenzo%20giacchina&rct=j
*/

register_activation_hook( __FILE__, 'installQuizdb');

function tabAdminQuiz() {
	add_options_page( 'QuizAnimation', 'QuizAnimation Setting', 'manage_options', 'my-unique-identifier', 'QuizOptions' );
}

function QuizOptions() {
	global $wpdb;
	$table_name = $wpdb->prefix . "quizAnimation";
	$aux = $wpdb->get_var("SELECT text FROM $table_name ORDER BY id DESC LIMIT 0,1");
	if ( !current_user_can( 'manage_options' ) )  {
		wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
	} 
	echo '<label>Domanda Quiz  </label><input id="quizQuestion" placeholder="'.$aux.'" type="text" /><input id="QuizsendAdmin" type="submit">';
}

function installQuizdb () {
	global $wpdb;
	$charset_collate = '';

	if ( ! empty( $wpdb->charset ) ) {
		$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
	}

	if ( ! empty( $wpdb->collate ) ) {
		$charset_collate .= " COLLATE {$wpdb->collate}";
	}

	$table_name = $wpdb->prefix . "quizAnimation";
	$sql = "CREATE TABLE $table_name (
	  id mediumint(9) NOT NULL AUTO_INCREMENT,
	  text text NOT NULL,
	  UNIQUE KEY id (id)
	) $charset_collate;";

	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	dbDelta( $sql );
}

function quiz_shortcode(){
	require ("template.php");
}

function insertScripts() {
	wp_enqueue_script( 'script-name', plugins_url('script.js', __FILE__), array(), '1.0.0', true );
}

function insertCss() {
	wp_enqueue_style( 'my-style', plugins_url( '/css/my-style.css', __FILE__ ), false, '1.0', 'all' );
}

function jsAdmin() { 
	$ajax_nonce = wp_create_nonce( "my-special-string" );
	?>
	<script type="text/javascript" >
		jQuery(document).ready(function(){
		    jQuery('#QuizsendAdmin').click(function(){
		        var clickBtnValue = jQuery('#quizQuestion').val();
				var data = {
					'action': 'my_action',
					'text': clickBtnValue
				};
		        jQuery.post(ajaxurl, data, function (response) {
		            alert(response);
		        });
		    });
		});
	</script> <?php
}

function my_action_callback() {
	global $wpdb;
	$table_name = $wpdb->prefix . "quizAnimation";
	$wpdb->insert( 
		$table_name, 
		array( 
			'text' => $_POST['text'] 
			) 
	);
	echo "Dato memorizzato con successo";
}



add_shortcode( 'quiz_test', 'quiz_shortcode' );
add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'admin_footer', 'jsAdmin' );
add_action( 'admin_menu', 'tabAdminQuiz' );
add_action( 'wp_enqueue_scripts', 'insertScripts' );
add_action('wp_head', 'insertCss');


?>